package com.ymx.darling.intercept;

import com.ymx.darling.exception.EntityException;
import com.ymx.darling.executor.Executor;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * 拦截管理器
 * @author 爱Java的小于
 */
public class InterceptManager {
    private static ExecutorInterceptor executorInterceptor;
    private static QueryWrapperInterceptor queryWrapperInterceptor;
    private static final Set<String> executorInterceptMethods = new HashSet<>();

    /**
     * 注册Executor的拦截器
     * @param executorInterceptor ei
     */
    public static void registerExecutorInterceptor(ExecutorInterceptor executorInterceptor) {
        InterceptManager.executorInterceptor = executorInterceptor;
        // 获取需要拦截的方法
        if (executorInterceptor != null) {
            Class<? extends ExecutorInterceptor> executorInterceptorClass = executorInterceptor.getClass();

            Set<String> methodSet = new HashSet<>();
            for (Method method : Executor.class.getMethods()) {
                methodSet.add(method.getName());
            }

            // 注册拦截注解中提供的方法
            if (executorInterceptorClass.isAnnotationPresent(ExecutorInterceptMethods.class)) {
                String[] methods = executorInterceptorClass.getAnnotation(ExecutorInterceptMethods.class).value();
                // 不提供任何方法默认拦截全部方法
                if (methods.length == 0) {
                    executorInterceptMethods.addAll(methodSet);
                } else {
                    for (String method : methods) {
                        if (!methodSet.contains(method)) {
                            throw new EntityException("注解'"+ExecutorInterceptMethods.class.getName()+"'中提供的方法'"+method+"'在'"+ Executor.class.getName()+"'中不存在");
                        }
                        executorInterceptMethods.add(method);
                    }
                }
            // 没有使用@ExecutorInterceptMethods注解则拦截全部方法
            } else {
                executorInterceptMethods.addAll(methodSet);
            }
        }
    }


    /**
     * 判断一个方法是否是需要拦截
     * @param methodName 根据方法名称判断
     * @return boolean
     */
    public static boolean isExecutorInterceptMethod(String methodName) {
        return executorInterceptMethods.contains(methodName);
    }


    /**
     * 判断Executor的拦截器是否被注册
     * @return boolean
     */
    public static boolean isExecutorInterceptorRegistered() {
        return executorInterceptor != null;
    }


    /**
     * 返回Executor的拦截器实例
     * @return ExecutorInterceptor
     */
    public static ExecutorInterceptor getExecutorInterceptor() {
        return executorInterceptor;
    }

    /**
     * 注册QueryWrapper的拦截器
     * @param queryWrapperInterceptor QueryWrapperInterceptor
     */
    public static void registerQueryWrapperInterceptor(QueryWrapperInterceptor queryWrapperInterceptor) {
        InterceptManager.queryWrapperInterceptor = queryWrapperInterceptor;
    }

    /**
     * 获取QueryWrapper的拦截器
     * @return QueryWrapperInterceptor
     */
    public static QueryWrapperInterceptor getQueryWrapperInterceptor() {
        return queryWrapperInterceptor;
    }

    /**
     * 判断QueryWrapper的拦截器是否注册
     * @return boolean
     */
    public static boolean isRegisterQueryWrapperInterceptor() {
        return queryWrapperInterceptor != null;
    }
}
